home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assembly / encrypt.lha / paz / Encrypt.s
Encoding:
Text File  |  1993-04-18  |  7.8 KB  |  358 lines

  1. ; Assembly settings should be executable.  Change D- to D+ for debugging
  2. ; and use M+ to expand MACROs into listing during debugging
  3. ; compiled DEVPAC software (works on all versions).
  4.  
  5. ;******************************************************************
  6. ;*                                  *
  7. ;* Data Encryption program by Steve Meredith 1991.  The program   *
  8. ;* prevents unauthorised reading or use of a file.  Encryption    *
  9. ;* does not support FASTMEM (ie. Extra memory).                   *
  10. ;* Assembled with DEVPAC 1 or 2.                            *
  11. ;*                                  *
  12. ;* Letters, questions and general stuff to me at :          *
  13. ;*    74 Pemberton Road, Winstanley, Wigan, WN3 6DA, UK      *             
  14. ;* or   28 Seaton Street, Middlesbrough, Cleveland, TS1 3NQ, UK      *
  15. ;******************************************************************
  16.  
  17. ;----- DOS calls --------------------
  18.  
  19. _LVOOpen    EQU    -30 
  20. _LVOClose    EQU    -36 
  21. _LVORead    EQU    -42 
  22. _LVOWrite    EQU    -48 
  23. _LVOOutput    EQU    -60 
  24. _LVOLock    EQU    -84 
  25. _LVOUnLock    EQU    -90 
  26. _LVOExamine    EQU    -102 
  27. _LVOInfo    EQU    -114
  28.  
  29. ;----- EXEC calls -------------------
  30.  
  31. _LVOAllocMem    EQU    -198 
  32. _LVOFreeMem    EQU    -210 
  33. _LVOOpenLibrary        EQU    -552
  34. _LVOCloseLibrary    EQU    -414 
  35.  
  36. ;----- MISC Equates -----------------
  37.  
  38. _SysBase    EQU    4
  39. MEMF_CHIP    EQU    1<<1
  40. MEMF_CLEAR    EQU    1<<16
  41. MODE_OLDFILE    EQU    1005
  42. MODE_NEWFILE    EQU    1006
  43.  
  44. ;----- PRINT Macro equates ----------
  45.  
  46. Free        EQU 2
  47. Exit            EQU 1
  48. Comeback        EQU 0
  49.  
  50. ;----- CALL EXEC MACRO --------------
  51.  
  52. CALLEXEC MACRO
  53.     move.l    (_SysBase).w,a6
  54.     jsr    _LVO\1(a6)
  55.     ENDM
  56.  
  57. ;----- CALL DOS MACRO ---------------
  58.  
  59. CALLDOS    MACRO
  60.     move.l    _DOSBase,a6
  61.     jsr    _LVO\1(a6)
  62.     ENDM
  63.  
  64. ;----- PRINT TEXT MACRO -------------
  65.  
  66. PRINT MACRO
  67.     CALLDOS    Output
  68.     move.l    #\1,d2
  69.     move.l    #53,d3
  70.     CALLDOS Write
  71.     move.b    #\2,d0
  72.     cmp.b    #Exit,d0
  73.     beq    clib
  74.     cmp.b    #Free,d0
  75.     beq    FreeMemory
  76.     ENDM
  77.  
  78. ;----- MAIN PROGRAM BEGINS HERE -----
  79.  
  80. Open_Dos_Lib
  81.     move.l    a0,a4
  82.     move.l    d0,a0
  83.       move.l    #dosname,a1
  84.     CALLEXEC OpenLibrary        ; Open library
  85.     tst.l    d0
  86.     beq    Quit            ; Whoops, error.
  87.     move.l    d0,_DOSBase        ; Ok. Keep DOSBase
  88.     move.l    a4,a0
  89.     
  90. Check_Input
  91.     move.b    (a0),d0            ; Check if user just presses
  92.     cmp.b    #$0a,d0            ; [RETURN]
  93.     bne.s    Split_Args
  94.     PRINT    UsageTxt,Comeback
  95.     PRINT    UsageTxt2,Exit        ; Print usage and quit
  96.  
  97. Split_Args
  98.     PRINT    Working,Comeback
  99.     move.l    a4,a0
  100.     bsr    StringSplit        ; Get password
  101.     move.l    d1,PassWORD
  102.     lea.l    2(a0),a0
  103.     bsr    StringSplit        ; Get passcode
  104.     move.l    d1,PassCODE
  105.     lea.l    2(a0),a0
  106.     move.l    a0,FileName        ; Get filename
  107.  
  108. FindSpc    lea.l    1(a0),a0        ; NULL terminate filename
  109.     cmp.b    #$0A,(a0)
  110.     bne    FindSpc
  111.     move.b    #0,(a0)
  112.  
  113. Get_Lock
  114.     move.l    FileName,d1        ; Check if file exists
  115.     move.l    #-2,d2
  116.     CALLDOS Lock    
  117.     move.l    d0,MyLock
  118.  
  119.     
  120. Alloc_InfoMem
  121.     move.l    #256,d0
  122.     move.l    #MEMF_CHIP!MEMF_CLEAR,d1 ; I want CHIP memory
  123.     CALLEXEC AllocMem          ;
  124.     tst.l    d0             ;
  125.     bne.s    Get_DiskInfo         ; Successful
  126.     PRINT    NoMemory,Exit
  127.     
  128. Get_DiskInfo            ; Check WRITEPROTECT ENABLED
  129.     move.l    d0,TOP
  130.     move.l    d0,d2
  131.     move.l    MyLock,d1
  132.     CALLDOS Info
  133.     move.l    TOP,a1
  134.     move.l    8(a1),a1
  135.     move.l    a1,d0
  136.     cmp.b    #$52,d0        ; Check ENABLED
  137.     beq.s    Get_FileInfo
  138.     move.l    TOP,a1
  139.     move.l    #256,d0        ; Size of memory allocated
  140.     moveq    #0,d1
  141.     CALLEXEC FreeMem    
  142.     move.l    MyLock,d1
  143.     CALLDOS UnLock
  144.     PRINT    DiskErr,Exit    ; WRITE PROTECTED exit
  145.  
  146. Get_FileInfo            ; Get information on file
  147.     move.l    TOP,a5
  148.     move.l    MyLock,d1
  149.     move.l    a5,d2
  150.     CALLDOS Examine
  151.  
  152. Free_Lock
  153.     move.l    MyLock,d1
  154.     CALLDOS UnLock
  155.  
  156. Get_Size
  157.     move.l    124(a5),a5
  158.     move.l    a5,FileSize    ; Get filesize
  159.  
  160. Free_InfoMem
  161.     move.l    TOP,a1
  162.     move.l    #256,d0        ; Size of memory allocated
  163.     moveq    #0,d1
  164.     CALLEXEC FreeMem    
  165.  
  166. Allocate_Memory
  167.     move.l    FileSize,d0
  168.     add.b    #$10,d0
  169.     move.l    d0,TotalSize
  170.     move.l    #MEMF_CHIP!MEMF_CLEAR,d1 ; I want CHIP memory
  171.     CALLEXEC AllocMem          ;
  172.     tst.l    d0             ;
  173.     bne    MemOK             ; Successful
  174.     PRINT    NoMemory,Exit
  175.     
  176. MemOK    move.l    d0,TOP            ; Set save block addresses
  177.     add    #16,d0            ;
  178.     move.l    d0,MIDDLE        ;
  179.     add    #16,d0            ;
  180.     move.l    d0,BOTTOM        ;
  181.     
  182. ; ***** OPEN DATAFILE *****
  183.     move.l    #0,d0
  184.     move.l    FileName,d1        ; Filename address to d1
  185.     move.l    #MODE_OLDFILE,d2    ; 
  186.     CALLDOS Open            ;         
  187.     tst.l    d0
  188.     bne    Readfle
  189.     PRINT    Error1,Free        ; Couldn't get file handle
  190.     
  191. ; ***** READ DATAFILE INTO ALLOCATED MEMORY (at MIDDLE address) *****
  192.  
  193. Readfle    move.l    d0,FHandle    
  194.     move.l    MIDDLE,d2        ; Allocated-memory space  to d2        
  195.     move.l    FileSize,d3        ; File size to d3
  196.     CALLDOS Read            ; Read data
  197.     move.l    d0,FileSize
  198.     
  199.     move.l    MIDDLE,a1        ; Old or New file?
  200.     cmp.l    #'Cryp',(a1)
  201.     beq    CheckPass        ; old file, check pass
  202.     
  203. PrepNew    PRINT    NewTxt,Comeback
  204.     move.l    TOP,a1
  205.     move.l    #'Cryp',(a1)+
  206.     move.l    #'t-2 ',(a1)+
  207.     move.l    #'(c) ',(a1)+
  208.     lea.l    PassWORD,a2        ; Create diskcode
  209.     move.l    (a2),d1
  210.     lea.l    PassCODE,a2
  211.     move.l    (a2),d2
  212.     EOR.l    d1,d2
  213.     not.l    d2
  214.     move.l    d2,(a1)
  215.     move.l    MIDDLE,CryptAddress
  216.     move.l    TOP,FileAddress
  217.     move.b    #$10,ExtraBytes
  218.     bra    Cr_DeCrypt
  219.     
  220. CheckPass
  221.     lea.l    12(a1),a1
  222.     move.l    (a1),d0
  223.     lea.l    PassCODE,a1
  224.     move.l    (a1),d1
  225.     EOR.l    d1,d0
  226.     not.l    d0
  227.     lea.l    PassWORD,a1
  228.     move.l    (a1),d1
  229.     cmp.l    d1,d0
  230.     beq    PassOK
  231.     move.l    FHandle,d1        ; FILEHANDLE to d1
  232.     CALLDOS Close            ; Close file
  233.     PRINT   UnAuth,Free        ; Wrong code! exit.
  234.     
  235. PassOK    PRINT    OldTxt,Comeback
  236.     add.l    #-$10,FileSize
  237.     move.l  BOTTOM,CryptAddress
  238.     move.l    BOTTOM,FileAddress
  239.     
  240. Cr_DeCrypt
  241.     lea.l    PassWORD,a1
  242.     move.l    (a1),d2
  243.     not.l    d2
  244.     move.l    CryptAddress,a1
  245.     move.l    FileSize,d0
  246. Crypt                    ; Crypt/DeCrypt datafile    
  247.     eor.l    d2,(a1)            ; Xor password with data
  248.     add.l    #4,a1            ; Encrypt every longword
  249.     sub.l    #4,d0            ; Decrement file-size counter
  250.     tst.l    d0
  251.     bgt.s    Crypt
  252.     eor.l    d2,(a1)
  253.  
  254. Save    move.l    FHandle,d1
  255.     CALLDOS Close
  256.  
  257.     move.l    FileName,d1        ; Filename address to a1
  258.     move.l    #MODE_NEWFILE,d2     ; 
  259.     CALLDOS Open            ;         
  260.     move.l    d0,FHandle        ; Store file handle.
  261.         
  262.     move.l    FHandle,d1
  263.     move.l    FileAddress,d2
  264.     move.l    FileSize,d3
  265.     add.b    ExtraBytes,d3
  266.     CALLDOS Write
  267.     move.l    FHandle,d1        ; FILEHANDLE to d1
  268.     CALLDOS Close            ; Close file
  269.     PRINT    Finished,Comeback
  270.     
  271. FreeMemory
  272.     move.l    TOP,a1
  273.     move.l    TotalSize,d0        ; Size of memory allocated
  274.     moveq    #0,d1
  275.     CALLEXEC FreeMem    
  276.  
  277. clib    move.l    _DOSBase,a1        ; Close DOS library
  278.     CALLEXEC CloseLibrary
  279.  
  280. Quit    rts    
  281.  
  282. ;*********************************************************************
  283. ;*                                     *
  284. ;* Sub-routine to split users CODE and PASS words and to extend the  *
  285. ;* CODE or PASS to 4 characters if need be.                 *
  286. ;*                                     *
  287. ;*********************************************************************
  288.  
  289. StringSplit
  290.     clr.l    d2
  291.     clr.l    d1
  292.     move.b    (a0),-(sp)
  293.     moveq    #3,d0
  294. Push    lea.l    1(a0),a0
  295.     cmp.b    #$20,(a0)
  296.     beq    Extend
  297.     
  298.     move.b    (a0),-(sp)
  299.     subq    #1,d0
  300.     tst.b    d0
  301.     bne    Push
  302.     
  303. Zoom    lea.l    1(a0),a0
  304.     cmp.b    #$20,(a0)
  305.     bne    Zoom
  306.     lea.l    -1(a0),a0
  307.     bra    Pullstk
  308.     
  309. Extend    lea.l    -1(a0),a0    ; pad last letter to 4 character word 
  310. Again    move.b    (a0),-(sp)
  311.     subq    #1,d0
  312.     tst.b    d0
  313.     bne    Again
  314.     
  315. Pullstk    move.l    #0,d1        ; Pull characters from stack
  316.     move.b    (sp)+,d1
  317.     mulu.w    #$100,d1
  318.     move.b    (sp)+,d1
  319.     swap.w    d1
  320.     move.b    (sp)+,d2
  321.     mulu.w    #$100,d2
  322.     move.b    (sp)+,d2
  323.     
  324.     add.l    d2,d1        ; 4 characters into d1
  325.     rts
  326.     
  327. ;************************* STORAGE *********************************
  328.  
  329. dosname        dc.b     'dos.library',0
  330. _DOSBase    dc.l    0    ; DOSbase address
  331. PassWORD    dc.l    0    ;
  332. PassCODE    dc.l    0    ;
  333. FileName    dc.l    0    ;
  334. CryptAddress    dc.l    0    ;
  335. FileAddress    dc.l    0    ;
  336. FileSize    dc.l    0    ;
  337. FHandle        dc.l    0    ;
  338. MIDDLE        dc.l    0    ;
  339. TOP        dc.l    0    ;
  340. BOTTOM        dc.l    0    ;
  341. TotalSize    dc.l    0    ;
  342. ExtraBytes    dc.b    0    ;
  343. MyLock        dc.l    0    ;
  344.  
  345. DiskErr        dc.b    'Disk Error : Please Write-Enable?.                  ',10
  346. NoMemory    dc.b    'Cant Allocate Memory - Kill External Sources/ReBoot ',10
  347. Error1        dc.b    'Unable to open file/incorrect parameters.           ',10
  348. UnAuth        dc.b    'Operation Aborted - Incorrect keys.                 ',10
  349. Finished    dc.b    'Operation complete.                                 ',10
  350. UsageTxt2    dc.b    'PassCode : MAX 4 chars in length. No FASTMEM support',10
  351. UsageTxt    dc.b    '   Usage : Crypt [PassWord] [PassCode] [filename]   ',10
  352. OldTxt        dc.b    '->De-Crypting file.                                 ',10
  353. NewTxt        dc.b    '->Crypting ',27,'[3mnewfile.',27,'[0m','                         ',10
  354. Working        dc.b    27,'[7m',10,'Encryption  by S.Meredith.',10,27,'[0m','->Checking File ',10
  355.    
  356.         even
  357.  
  358.